package com.xyazm.order.statemachine.action;

import com.xyazm.common.enums.OrderStatusChangeEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单状态机 获取action工厂类
 * @author xuyou
 * @date 2023年01月30日 20:36
 */
@Component
public class OrderActionFactory {

    @Resource
    private List<OrderStateAction<?>> actions;

    /**
     * 根据要执行的事件获取对应的action
     * @author xuyou
     * 2023-01-30 21:47
     * @param event
    */
    public OrderStateAction<?> getAction(OrderStatusChangeEnum event) {
        for (OrderStateAction<?> action : actions) {
            if (action.event() == null) {
                throw new IllegalArgumentException("event 返回值不能为空：" + action.getClass().getSimpleName());
            }
            if (action.event().equals(event)) {
                return action;
            }
        }
        return null;
    }

}
